package cn.iocoder.yudao.module.data.third.eastMoney.response;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.data.third.IBaseResponse;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author pokbrin
 * @date 2023-07-06 13:23
 */
@NoArgsConstructor
@Data
public class EastMoneyStockFenShiResponse implements IBaseResponse {
    /**
     * 返回码，0 表示成功。
     */
    private Integer rc;
    private Integer rt;
    private Long svr;
    private Integer lt;
    private Integer full;
    private String dlmkts;
    private InnerData data;

    @Data
    public static class InnerData {
        //股票代码
        private String code;
        //市场类型
        private Integer market;
        private Integer type;
        private Integer status;
        private String name;
        private String decimal;
        private BigDecimal preSettlement;
        private BigDecimal preClose;
        private String beticks;
        private Integer trendsTotal;
        //1688621775
        private String time;
        private Integer kind;
        private BigDecimal prePrice;
        private List<String> trends;


    }

    @Data
    public static class Holder {

        public Holder(EastMoneyStockFenShiResponse response) {
            this.response = response;
            this.getTrendVOS();
        }

        private EastMoneyStockFenShiResponse response;

        private List<TrendVO> trendVOS;


        public Pair<LocalDate, LocalTime> getLocalDateTime() {
            LocalDateTime time = trendVOS.get(trendVOS.size() - 1).getTrendTime();
            //获取当前服务器的秒
            return Pair.of(time.toLocalDate(), time.toLocalTime().plusSeconds(LocalTime.now().getSecond()));
        }

        /**
         * 获取开盘价
         *
         * @return {@link BigDecimal}
         */
        public BigDecimal getOpen() {
            return trendVOS.get(0).getOpen();
        }

        /**
         * 获取最新价
         *
         * @return {@link BigDecimal}
         */
        public BigDecimal getClose() {
            return trendVOS.get(trendVOS.size() - 1).getPrice();
        }

        /**
         * 获取最高价
         *
         * @return {@link BigDecimal}
         */
        public BigDecimal getHigh() {
            return trendVOS.stream().map(TrendVO::getHigh).max(BigDecimal::compareTo).orElse(BigDecimal.ZERO);
        }

        /**
         * 获取最低价
         *
         * @return {@link BigDecimal}
         */
        public BigDecimal getLow() {
            return trendVOS.stream().map(TrendVO::getLow).min(BigDecimal::compareTo).orElse(BigDecimal.ZERO);
        }

        /**
         * 获取最新均价
         *
         * @return {@link BigDecimal}
         */
        public BigDecimal getAvgPrice() {
            return trendVOS.get(trendVOS.size() - 1).avgPrice;
        }

        /**
         * 成交量 和 成交额
         *
         * @return
         */
        public Pair<Integer, BigDecimal> getVolAmount() {
            int vol = 0;
            BigDecimal amount = BigDecimal.ZERO;
            for (TrendVO vo : trendVOS) {
                vol += vo.vol;
                amount = amount.add(vo.amount);
            }
            return new Pair<>(vol, amount);
        }


        public List<TrendVO> getTrendVOS() {
            if (CollUtil.isNotEmpty(this.trendVOS)) {
                return this.trendVOS;
            }
            return this.trendVOS = CollUtil.emptyIfNull(response.data.trends)
                    .stream()
                    .map(line -> StrUtil.split(line, ","))
                    .map(lineDates -> {
                        TrendVO tv = new TrendVO();
                        tv.setTrendTime(LocalDateTimeUtil.parse(lineDates.get(0), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
                        tv.setOpen(new BigDecimal(lineDates.get(1)));
                        tv.setPrice(new BigDecimal(lineDates.get(2)));
                        tv.setHigh(new BigDecimal(lineDates.get(3)));
                        tv.setLow(new BigDecimal(lineDates.get(4)));
                        tv.setVol(Integer.valueOf(lineDates.get(5)));
                        tv.setAmount(new BigDecimal(lineDates.get(6)));
                        tv.setAvgPrice(new BigDecimal(lineDates.get(7)));
                        return tv;
                    }).collect(Collectors.toList());

        }
    }


    /**
     * trends 解析对象
     *
     * @author pokbrin
     * @date 2023-07-19
     */
    @Data
    public static class TrendVO {
        /**
         * 时间(精确到分钟) yyyy-MM-dd HH:mm
         */
        private LocalDateTime trendTime;
        /**
         * 开盘价
         */
        private BigDecimal open;
        /**
         * 收盘价
         */
        private BigDecimal price;
        /**
         * 最高价
         */
        private BigDecimal high;
        /**
         * 最低价
         */
        private BigDecimal low;
        /**
         * 成交量(手)
         */
        private Integer vol;
        /**
         * 成交额(元)
         */
        private BigDecimal amount;
        /**
         * 均价
         */
        private BigDecimal avgPrice;
    }
}
